﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PayexpDAL;
using Utilities;

namespace PayexpDAL
{
    public static class SettingsAndStaticListLoader
    {
        static SettingsAndStaticListLoader()
        {
            LoadStaticList();
            LoadExpenseApprovers();
            LoadBankAccounts();
            LoadAppSettings();
            LoadHeadDefaultValues();
        }

        private static Dictionary<string, List<StaticList>> staticListMap = new Dictionary<string, List<StaticList>>();
        
        public static Dictionary<string, List<StaticList>> StaticListMap
        {
            get { return SettingsAndStaticListLoader.staticListMap; }
        }

        private static List<StaticList> transactionModeList = new List<StaticList>();

        public static List<StaticList> TransactionModeList
        {
            get { return SettingsAndStaticListLoader.transactionModeList; }
        }

        private static List<StaticList> bankNameList = new List<StaticList>();

        public static List<StaticList> BankNameList
        {
            get { return SettingsAndStaticListLoader.bankNameList; }
        }

        private static List<StaticList> chequeStatusList = new List<StaticList>();

        public static List<StaticList> ChequeStatusList
        {
            get { return SettingsAndStaticListLoader.chequeStatusList; }
        }

        private static List<StaticList> headList = new List<StaticList>();

        public static List<StaticList> HeadList
        {
            get { return SettingsAndStaticListLoader.headList; }
        }

        private static List<ExpenseApprover> expenseApproverList = new List<ExpenseApprover>();

        public static List<ExpenseApprover> ExpenseApproverList
        {
            get { return SettingsAndStaticListLoader.expenseApproverList; }
        }

        private static List<BankAccount> bankAccountList = new List<BankAccount>();

        public static List<BankAccount> BankAccountList
        {
            get { return SettingsAndStaticListLoader.bankAccountList; }
        }

        private static List<ApplicationSetting> applicationSettingList = new List<ApplicationSetting>();

        public static List<ApplicationSetting> ApplicationSettingList
        {
            get { return SettingsAndStaticListLoader.applicationSettingList; }
        }

        private static List<HeadDefaultValue> headDefaultValueList = new List<HeadDefaultValue>();

        public static List<HeadDefaultValue> HeadDefaultValueList
        {
            get { return headDefaultValueList; }
            set { headDefaultValueList = value; }
        }

        private static List<StaticList> bankAccountsStaticList = new List<StaticList>();

        public static List<StaticList> BankAccountsStaticList
        {
            get { return SettingsAndStaticListLoader.bankAccountsStaticList; }
        }

        public static void LoadStaticList()
        {
            try
            {
                DataAccessHandler dataAccessHandler = new DataAccessHandler();
                staticListMap.Clear();
                transactionModeList.Clear();
                bankNameList.Clear();
                chequeStatusList.Clear();
                headList.Clear();

                using (AlfaRomeoEntities context = new AlfaRomeoEntities())
                {
                    List<string> distinctListTypes = dataAccessHandler.GetDistinctListTypes(context);
                    foreach (string listType in distinctListTypes)
                    {
                        if (!staticListMap.ContainsKey(listType))
                        {
                            List<StaticList> listValues = new List<StaticList>();
                            listValues.AddRange(dataAccessHandler.GetListValuesByListType(listType, context));
                            listValues.Sort(new SortStaticList());
                            staticListMap.Add(listType, listValues);
                        }
                    }
                }

                transactionModeList.AddRange(StaticListMap[Constants.TableConstants.StaticListConstants.transactionmode]);
                bankNameList.AddRange(StaticListMap[Constants.TableConstants.StaticListConstants.bankname]);
                chequeStatusList.AddRange(StaticListMap[Constants.TableConstants.StaticListConstants.chequestatus]);
                headList.AddRange(StaticListMap[Constants.TableConstants.StaticListConstants.head]);
            }
            catch
            {
                if (!staticListMap.ContainsKey(Constants.TableConstants.StaticListConstants.bankname))
                {
                    staticListMap.Add(Constants.TableConstants.StaticListConstants.bankname, new List<StaticList>());
                }

                if (!staticListMap.ContainsKey(Constants.TableConstants.StaticListConstants.bankname))
                {
                    staticListMap.Add(Constants.TableConstants.StaticListConstants.bankname, new List<StaticList>());
                }

                if (!staticListMap.ContainsKey(Constants.TableConstants.StaticListConstants.chequestatus))
                {
                    staticListMap.Add(Constants.TableConstants.StaticListConstants.chequestatus, new List<StaticList>());
                }

                if (!staticListMap.ContainsKey(Constants.TableConstants.StaticListConstants.doctype))
                {
                    staticListMap.Add(Constants.TableConstants.StaticListConstants.doctype, new List<StaticList>());
                }

                if (!staticListMap.ContainsKey(Constants.TableConstants.StaticListConstants.head))
                {
                    staticListMap.Add(Constants.TableConstants.StaticListConstants.head, new List<StaticList>());
                }

                if (!staticListMap.ContainsKey(Constants.TableConstants.StaticListConstants.transactionmode))
                {
                    staticListMap.Add(Constants.TableConstants.StaticListConstants.transactionmode, new List<StaticList>());
                }
            }
        }

        public static void LoadExpenseApprovers()
        {
            try
            {
                using (AlfaRomeoEntities context = new AlfaRomeoEntities())
                {
                    expenseApproverList.Clear();
                    expenseApproverList.AddRange(new DataAccessHandler().GetExpenseApproverList(context));
                }
            }
            catch { }
        }

        public static void LoadBankAccounts()
        {
            try
            {
                using (AlfaRomeoEntities context = new AlfaRomeoEntities())
                {
                    DataAccessHandler handler = new DataAccessHandler();
                    bankAccountList.Clear();
                    bankAccountsStaticList.Clear();
                    List<BankAccount> temp = new List<BankAccount>();
                    temp.AddRange(handler.GetAllBankAccounts(Utils.FinancialYear, context));
                    temp = handler.SetBankBalances(temp, context);

                    bankAccountList.AddRange(temp);

                    foreach (BankAccount bankAccount in bankAccountList)
                    {
                        StaticList bankAccountStaticListEntry = bankNameList.Where(t => t.ListItemCode.Equals(bankAccount.BankName)).FirstOrDefault();
                        if (bankAccountStaticListEntry != null)
                        {
                            bankAccountsStaticList.Add(bankAccountStaticListEntry);
                        }
                    }
                }
            }
            catch { }
        }

        public static void LoadAppSettings()
        {
            try
            {
                using (AlfaRomeoEntities context = new AlfaRomeoEntities())
                {
                    applicationSettingList.Clear();
                    applicationSettingList.AddRange(new DataAccessHandler().GetAllApplicationSettings(context));
                }
            }
            catch { }
        }

        public static void LoadHeadDefaultValues()
        {
            try
            {
                using (AlfaRomeoEntities context = new AlfaRomeoEntities())
                {
                    headDefaultValueList.Clear();
                    headDefaultValueList.AddRange(new DataAccessHandler().GetAllHeadDefaultValues(context));
                }
            }
            catch { }
        }

        public static string GetAppSettingValue(string settingName)
        {
            ApplicationSetting requiredApplicationSetting = applicationSettingList.Where(t => t.SettingName.Equals(settingName)).FirstOrDefault();
            if (requiredApplicationSetting != null)
            {
                return requiredApplicationSetting.SettingValue;
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
